// 初始化tinymce编辑器
tinymce.init({
    selector: '#editor',
    height: 500,
    language: 'zh_CN',
    base_url: '/static/tinymce',
    license_key: 'gpl',
    plugins: [
        'advlist',
        'autolink',
        'lists',
        'link',
        'image',
        'charmap',
        'preview',
        'anchor',
        'searchreplace',
        'visualblocks',
        'code',
        'fullscreen',
        'insertdatetime',
        'media',
        'table',
        'help',
        'wordcount'
    ],
    toolbar: [
        'undo redo | formatselect | bold italic backcolor',
        'alignleft aligncenter alignright alignjustify',
        'bullist numlist outdent indent',
        'link image media | removeformat code preview'
    ].join(' | '),
    
    // 修改图片上传配置
    images_upload_handler: function(blobInfo, progress) {
        return new Promise(function(resolve, reject) {
            var formData = new FormData();
            formData.append('file', blobInfo.blob(), blobInfo.filename());
            
            var index = layer.load(1, {
                shade: [0.1,'#fff']
            });
            
            $.ajax({
                url: '{{urlfor "ManagerController.TinymceUpload"}}', // 图片上传接口
                type: 'POST',
                data: formData,
                processData: false,
                contentType: false,
                success: function(res) {
                    layer.close(index);
                    if(res.errcode === 0) {
                        resolve(res.data.location);
                    } else {
                        reject(res.message || '上传失败');
                    }
                },
                error: function() {
                    layer.close(index);
                    reject('上传失败，请稍后重试');
                },
                xhr: function() {
                    var xhr = new XMLHttpRequest();
                    xhr.upload.onprogress = function(e) {
                        progress(e.loaded / e.total * 100);
                    }
                    return xhr;
                }
            });
        });
    },
    
    // 图片工具栏
    image_title: true,      // 启用图片标题
    image_caption: true,    // 启用图片描述
    image_advtab: true,     // 启用高级选项卡
    
    // 设置图片默认样式
    image_class_list: [
        {title: '无样式', value: ''},
        {title: '响应式', value: 'img-responsive'},
        {title: '圆角', value: 'img-rounded'},
        {title: '圆形', value: 'img-circle'}
    ],
    
    // 其他配置
    relative_urls: false,
    remove_script_host: false,
    convert_urls: true,
    
    setup: function(editor) {
        editor.on('change', function() {
            editor.save();
        });
    }
});
// 生成唯一标识
function generateIdentify() {
    var index = layer.load(1, {
        shade: [0.1,'#fff']
    });
    
    $.ajax({
        url: '/bookchat/api/v1/articles/generate-identify', // 生成唯一标识的接口
        type: 'get',
        dataType: 'json',
        success: function(res) {
            layer.close(index);
            if(res.message === "操作成功") {
                $('input[name="identify"]').val(res.data);
            } else {
                layer.msg(res.message || '生成失败');
            }
        },
        error: function() {
            layer.close(index);
            layer.msg('生成失败，请稍后重试');
        }
    });
}